package com.wjf.birdfish.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;


public class OnlineUserBindingListener implements HttpSessionBindingListener {
	private String loginName;

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public OnlineUserBindingListener(String loginName) {
		this.loginName = loginName;
	}

	public void valueBound(HttpSessionBindingEvent event) {
		HttpSession session = event.getSession();
		ServletContext application = session.getServletContext();
		// 把用户名放入在线列表
		List<String> onlineUserList =  (List<String>) application.getAttribute("onlineUserList");
		
		// 第一次使用前，需要初始化
		if (onlineUserList == null) {
			onlineUserList = new ArrayList<String>();
		}
		onlineUserList.add(this.loginName);
		application.setAttribute("onlineUserList", onlineUserList);

	}
	/**
	 * 解除
	 * */
	public void valueUnbound(HttpSessionBindingEvent event) {
		HttpSession session = event.getSession();
		ServletContext application = session.getServletContext();
		List<String> onlineUserList =  (List<String>) application.getAttribute("onlineUserList");
		if(onlineUserList != null){
			onlineUserList.remove(this.loginName);
			//System.out.println("卸载"+this.loginName);
		}
	}

}

